package com.ruoyi.net.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.XmlUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.ElementPortEth;
import com.ruoyi.net.domain.ElementPortMode;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.Device;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.net.mapper.ElementPortMtnFgMapper;
import com.ruoyi.net.domain.ElementPortMtnFg;
import com.ruoyi.net.service.IElementPortMtnFgService;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 网元-接口-mtnfg接口Service业务层处理
 * 
 * @author net
 * @date 2024-11-17
 */
@Service
public class ElementPortMtnFgServiceImpl implements IElementPortMtnFgService 
{
    @Autowired
    private ElementPortMtnFgMapper elementPortMtnFgMapper;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private DeviceManager deviceManager;
    /**
     * 查询网元-接口-mtnfg接口
     * 
     * @param id 网元-接口-mtnfg接口主键
     * @return 网元-接口-mtnfg接口
     */
    @Override
    public ElementPortMtnFg selectElementPortMtnFgById(String id)
    {
        return elementPortMtnFgMapper.selectElementPortMtnFgById(id);
    }

    /**
     * 查询网元-接口-mtnfg接口列表
     * 
     * @param elementPortMtnFg 网元-接口-mtnfg接口
     * @return 网元-接口-mtnfg接口
     */
    @Override
    public List<ElementPortMtnFg> selectElementPortMtnFgList(ElementPortMtnFg elementPortMtnFg)
    {
        return elementPortMtnFgMapper.selectElementPortMtnFgList(elementPortMtnFg);
    }

    @Override
    public List<ElementPortMtnFg> init(ElementPortMtnFg elementPortMtnFg)
    {
        List<ElementPortMtnFg> elementPortMtnFgs = new ArrayList<>();
        if (StringUtils.isEmpty(elementPortMtnFg.getElementId())) {
            return elementPortMtnFgs;
        }
        List<ElementPortMtnFg> readElements = readDevice(elementPortMtnFg);
        List<ElementPortMtnFg> dbElements = elementPortMtnFgMapper.selectElementPortMtnFgList(elementPortMtnFg);
        List<ElementPortMtnFg> uncommonElements = readElements.stream().filter(readBack -> dbElements.stream()
                        .noneMatch(dbElement -> readBack.getTerminalName().substring(StringUtils.indexOfAny(readBack.getTerminalName(), "/") - 1)
                                .equalsIgnoreCase(dbElement.getTerminalName().substring(StringUtils.indexOfAny(dbElement.getTerminalName(), "/") - 1))))
                .collect(Collectors.toList());
        // 不相同
        for (ElementPortMtnFg uncommonElement : uncommonElements) {
            uncommonElement.setId(IdUtils.simpleUUID());
            uncommonElement.setStatus("1");
            uncommonElement.setTerminalName(uncommonElement.getTerminalName());
            uncommonElement.setElementId(elementPortMtnFg.getElementId());
            uncommonElement.setCreateBy(SecurityUtils.getUsername());
            uncommonElement.setCreateTime(DateUtils.getNowDate());
            elementPortMtnFgMapper.insertElementPortMtnFg(uncommonElement);
        }
        return elementPortMtnFgMapper.selectElementPortMtnFgList(elementPortMtnFg);
    }

    /**
     * 读取端口信息
     * @param elementPortMtnFg
     * @return
     */
    private List<ElementPortMtnFg> readDevice(ElementPortMtnFg elementPortMtnFg) {
        List<ElementPortMtnFg> elementPortMtnFgs = new ArrayList<>();
        StringBuilder xml = new StringBuilder();
        xml.append(
                "<if xmlns=\"http://fiberhome.com/fhnw/yang/interface/fos-interface-common\">" +
                        "    <interfaces>" +
                        "      <interface xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\">" +
                        "        <type>MTN-FGPHY</type>" +
                        "        <enabled>enable</enabled>" + // 主接口
                        "      </interface>" +
                        "    </interfaces>" +
                        "  </if>");
        NetElement netElement = netElementService.getCacheObject(elementPortMtnFg.getElementId());
        Device device = deviceManager.getDevice(netElement.getIp());
        if (device.isConnected()) {
            String rpcReply = deviceManager.getConfig(xml.toString());

            // 解析xml
            NodeList list = XmlUtils.findNetConfXmlNodes(rpcReply, "//interfaces/interface");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    ElementPortMtnFg entity = new ElementPortMtnFg();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值
                    entity.setElementId(elementPortMtnFg.getElementId());
                    entity.setTerminalName(XmlUtils.getXmlLabelValue(node, "name"));
                    entity.setStatus("1"); // 从设备读取到的配置，设置状态为未激活
                    elementPortMtnFgs.add(entity);
                }
            }
        }
        return elementPortMtnFgs;
    }

    /**
     * 新增网元-接口-mtnfg接口
     * 
     * @param elementPortMtnFg 网元-接口-mtnfg接口
     * @return 结果
     */
    @Override
    public int insertElementPortMtnFg(ElementPortMtnFg elementPortMtnFg)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(elementPortMtnFg.getId())) {
            elementPortMtnFg.setId(uuid);
        }
        elementPortMtnFg.setCreateBy(SecurityUtils.getUsername());
        elementPortMtnFg.setCreateTime(DateUtils.getNowDate());
        elementPortMtnFg.setUpdateBy(SecurityUtils.getUsername());
        elementPortMtnFg.setUpdateTime(DateUtils.getNowDate());
        return elementPortMtnFgMapper.insertElementPortMtnFg(elementPortMtnFg);
    }

    /**
     * 修改网元-接口-mtnfg接口
     * 
     * @param elementPortMtnFg 网元-接口-mtnfg接口
     * @return 结果
     */
    @Override
    public int updateElementPortMtnFg(ElementPortMtnFg elementPortMtnFg)
    {
        elementPortMtnFg.setUpdateBy(SecurityUtils.getUsername());
        elementPortMtnFg.setUpdateTime(DateUtils.getNowDate());
        return elementPortMtnFgMapper.updateElementPortMtnFg(elementPortMtnFg);
    }

    /**
     * 批量删除网元-接口-mtnfg接口
     * 
     * @param ids 需要删除的网元-接口-mtnfg接口主键
     * @return 结果
     */
    @Override
    public int deleteElementPortMtnFgByIds(String[] ids)
    {
        return elementPortMtnFgMapper.deleteElementPortMtnFgByIds(ids);
    }

    /**
     * 删除网元-接口-mtnfg接口信息
     * 
     * @param id 网元-接口-mtnfg接口主键
     * @return 结果
     */
    @Override
    public int deleteElementPortMtnFgById(String id)
    {
        return elementPortMtnFgMapper.deleteElementPortMtnFgById(id);
    }
}
